package com.wang.wx.utils;

import com.google.common.hash.BloomFilter;
import com.wang.wx.constants.IndexConstants;
import com.wang.wx.entity.TwitterUser;
import com.wang.wx.entity.WxUser;


/**
 * @author wjh
 * @create  2024-08-02 16:01
 */
public class DynamicDataSplitterUtil {

    /**
     * 微信数据处理，布隆过滤器去重
     */
    public static WxUser wxUserBloomFilter(String line, BloomFilter<String> bloomFilter) {
        String[] parts = line.split(",");  // 按逗号字符分割数据
        WxUser wxUser = null;
        // 如果手机号都没有的话就不用入库了
        if (parts.length > IndexConstants.WxIndexConstants.PHONE_INDEX && parts[IndexConstants.WxIndexConstants.PHONE_INDEX] != null) {
            if (!bloomFilter.mightContain(parts[IndexConstants.WxIndexConstants.PHONE_INDEX])) {
                if (parts[IndexConstants.WxIndexConstants.PHONE_INDEX].trim().matches("^1[3456789]\\d{9}$")) {
                    wxUser = new WxUser();
                    bloomFilter.put(parts[IndexConstants.WxIndexConstants.PHONE_INDEX]);
                    wxUser.setPhone(parts[IndexConstants.WxIndexConstants.PHONE_INDEX].trim());
                    if (parts.length > IndexConstants.WxIndexConstants.SEX_INDEX && parts[IndexConstants.WxIndexConstants.SEX_INDEX] != null) {
                        wxUser.setSex(parts[IndexConstants.WxIndexConstants.SEX_INDEX]);
                        if (parts.length > IndexConstants.WxIndexConstants.WX_ID_INDEX && parts[IndexConstants.WxIndexConstants.WX_ID_INDEX] != null) {
                            wxUser.setWxId(parts[IndexConstants.WxIndexConstants.WX_ID_INDEX]);
                            if (parts.length > IndexConstants.WxIndexConstants.WX_PROVINCE && parts[IndexConstants.WxIndexConstants.WX_PROVINCE] != null) {
                                wxUser.setProvince(parts[IndexConstants.WxIndexConstants.WX_PROVINCE]);
                                if (parts.length > IndexConstants.WxIndexConstants.WX_CITY && parts[IndexConstants.WxIndexConstants.WX_CITY] != null) {
                                    wxUser.setCity(parts[IndexConstants.WxIndexConstants.WX_CITY]);
                                    if (parts.length > IndexConstants.WxIndexConstants.WX_NICKNAME && parts[IndexConstants.WxIndexConstants.WX_NICKNAME] != null) {
                                        wxUser.setNickname(parts[IndexConstants.WxIndexConstants.WX_NICKNAME]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return wxUser;
    }


    /**
     * 推特数据处理
     * @param line
     * @return
     */
    public static TwitterUser twitterUserBloomFilter(String line) {
        String[] split = line.split(" - ");
        TwitterUser twitterUser = null;
        if (split.length > IndexConstants.TwitterIndexConstants.EMAIL_INDEX && split[IndexConstants.TwitterIndexConstants.EMAIL_INDEX] != null) {
            if (split[IndexConstants.TwitterIndexConstants.EMAIL_INDEX].startsWith("Email:")) {
                String email = split[IndexConstants.TwitterIndexConstants.EMAIL_INDEX].replace("Email:", "");
                if (email.trim().matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$")) {
                    twitterUser = new TwitterUser();
                    twitterUser.setEmail(email.trim());
                    if (split.length > IndexConstants.TwitterIndexConstants.NAME_INDEX && split[IndexConstants.TwitterIndexConstants.NAME_INDEX] != null) {
                        String name = split[IndexConstants.TwitterIndexConstants.NAME_INDEX].replace("Name:", "");
                        twitterUser.setName(name.trim());
                    }
                    if (split.length > IndexConstants.TwitterIndexConstants.SCREEN_NAME_INDEX && split[IndexConstants.TwitterIndexConstants.SCREEN_NAME_INDEX] != null) {
                        String screenName = split[IndexConstants.TwitterIndexConstants.SCREEN_NAME_INDEX].replace("ScreenName:", "");
                        twitterUser.setScreenName(screenName.trim());
                    }
                    if (split.length > IndexConstants.TwitterIndexConstants.FOLLOWERS_INDEX && split[IndexConstants.TwitterIndexConstants.FOLLOWERS_INDEX] != null) {
                        String followers = split[IndexConstants.TwitterIndexConstants.FOLLOWERS_INDEX].replace("Followers:", "");
                        twitterUser.setFollowers(Integer.parseInt(followers.trim()));
                    }
                    if (split.length > IndexConstants.TwitterIndexConstants.CREATEd_AT_INDEX && split[IndexConstants.TwitterIndexConstants.CREATEd_AT_INDEX] != null) {
                        String createdAt = split[IndexConstants.TwitterIndexConstants.CREATEd_AT_INDEX].replace("Created At:", "");
                        twitterUser.setCreatedAt(DateFormatExample.parseLocalDateTime(createdAt.trim()));
                    }
                }
            }
            
        }
        return twitterUser;
    }
}
